﻿
// MFC-GraspPointDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "MFC-GraspPoint.h"
#include "MFC-GraspPointDlg.h"
#include "DialgHelp.h"
#include "afxdialogex.h"
#include "GrapScreen.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCGraspPointDlg 对话框


GrapScreen *gs = NULL;

CMFCGraspPointDlg::CMFCGraspPointDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMFCGraspPointDlg::IDD, pParent)
	, m_clickTime(0)
{
	ifGrapPoint=0;
	dstPoint.x=0;
	dstPoint.y=0;
	isScreenClick = 1;
	graspButton=0;
	
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_buttonHIcon = AfxGetApp()->LoadIcon(IDB_BITMAP_BUTTON);
	m_hAccelTable = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1));
}

CMFCGraspPointDlg::~CMFCGraspPointDlg()
{
	Shell_NotifyIcon(NIM_DELETE,&NotifyIcon);
}

void CMFCGraspPointDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Radio(pDX, IDC_RADIO_SCREEN, isScreenClick);
	DDX_Slider(pDX, IDC_SLIDER_TIME, m_clickTime);
	DDV_MinMaxInt(pDX, m_clickTime, 10, 10000);
}

BEGIN_MESSAGE_MAP(CMFCGraspPointDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_START, &CMFCGraspPointDlg::OnBnClickedButtonStart)
	ON_WM_MOUSEMOVE()
	ON_MESSAGE(WM_HOTKEY,OnHotKey)
	ON_MESSAGE(WM_NC,OnNotifyIcon)
	ON_WM_CREATE()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CMFCGraspPointDlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_HELP, &CMFCGraspPointDlg::OnBnClickedButtonHelp)
	ON_BN_CLICKED(IDC_BUTTON_MIN, &CMFCGraspPointDlg::changeMini)


	ON_WM_NOTIFYFORMAT()
	ON_WM_SYSCOMMAND()
	ON_BN_CLICKED(IDC_RADIO_SCREEN, &CMFCGraspPointDlg::OnBnClickedRadioScreen)
	ON_BN_CLICKED(IDC_RADIO_BUTTON, &CMFCGraspPointDlg::OnBnClickedRadiobutton)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_TIME, &CMFCGraspPointDlg::OnNMCustomdrawSliderTime)
	ON_NOTIFY(BCN_DROPDOWN, IDC_BUTTON_GRASP, &CMFCGraspPointDlg::OnBnDropDownButtonGrasp)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_EXIT, &CMFCGraspPointDlg::OnExit)
	ON_COMMAND(ID_COPY, &CMFCGraspPointDlg::OnCopy)
	ON_COMMAND(ID_SAVE, &CMFCGraspPointDlg::OnSave)
END_MESSAGE_MAP()


// CMFCGraspPointDlg 消息处理程序

BOOL CMFCGraspPointDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	if(!RegisterHotKey(m_hWnd, 1001, MOD_CONTROL, VK_F1))//注册热键
	{
		MessageBox(_T("热键冲突"));
	}
	if(!RegisterHotKey(m_hWnd, 1002, MOD_CONTROL, VK_F2))//注册热键
	{
		MessageBox(_T("热键冲突"));
	}
	if(!RegisterHotKey(m_hWnd, 1003, MOD_CONTROL, VK_F3))//注册热键
	{
		MessageBox(_T("热键冲突"));
	}
	if(!RegisterHotKey(m_hWnd, 1004, MOD_CONTROL, VK_F5))//注册热键
	{
		MessageBox(_T("热键冲突"));
	}

	dstCwnd=NULL;

	//加载位图
	HBITMAP hBitmap;
	HINSTANCE  hInstance   =   ::AfxGetInstanceHandle();
	hBitmap =  ::LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP_BUTTON) );
	((CButton*)GetDlgItem(IDC_BUTTON_GRASP))->SetBitmap(hBitmap);

	GetDlgItem(IDC_LOCATION)->ShowWindow(0);
	((CButton*)GetDlgItem(IDC_RADIO_SCREEN))->SetCheck(TRUE);//默认选中该单选按钮
	((CButton*)GetDlgItem(IDC_RADIO_BUTTON))->SetCheck(FALSE);

	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TIME))->SetRange(10,10000);
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TIME))->SetPos(50);

	//托盘
	NotifyIcon.cbSize=sizeof(NOTIFYICONDATA);
	NotifyIcon.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	NotifyIcon.hWnd=m_hWnd;
	lstrcpy(NotifyIcon.szTip,"MediaCraft EMMG");
	NotifyIcon.uCallbackMessage=WM_NC;
	NotifyIcon.uFlags=NIF_ICON | NIF_MESSAGE | NIF_TIP;
	Shell_NotifyIcon(NIM_ADD,&NotifyIcon);//添加图标

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CMFCGraspPointDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMFCGraspPointDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CMFCGraspPointDlg::OnBnClickedButtonStart()
{
	// TODO: 在此添加控件通知处理程序代码
	startGrapPoint();
}

void CMFCGraspPointDlg::startGrapPoint()
{
	if(ifGrapPoint)
	{
		ifGrapPoint=0;
		SetDlgItemTextA(IDC_BUTTON_START,"开始");
		KillTimer(0);
	}
	else
	{
		ifGrapPoint=1;
		GetDlgItem(IDC_LOCATION)->ShowWindow(1);
		SetDlgItemTextA(IDC_BUTTON_START,"暂停");
		SetTimer(0,200,0);
	}
}

void CMFCGraspPointDlg::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CDialogEx::OnMouseMove(nFlags, point);
}


BOOL CMFCGraspPointDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (m_hAccelTable) {
		if (::TranslateAccelerator(m_hWnd, m_hAccelTable, pMsg)) {
			return(TRUE);
		}
	}
	switch(pMsg->message)
	{
	case(WM_HOTKEY):
		if(pMsg->wParam == 1001)//如果是自定义的消息，就处理。与OnHotKey功能相近。
		{
			GetCursorPos(&dstPoint);//获得鼠标位置
			dstCwnd = WindowFromPoint(dstPoint)/*->GetForegroundWindow()*/;

		}
		if(pMsg->wParam == 1002)//如果是自定义的消息，就处理。与OnHotKey功能相近。
		{
			SetTimer(1,clickTime,0);
		}
		if(pMsg->wParam == 1003)//如果是自定义的消息，就处理。与OnHotKey功能相近。
		{
			KillTimer(1);

		}
		if(pMsg->wParam == 1004)//如果是自定义的消息，就处理。与OnHotKey功能相近。
		{
			
			if (!gs)
			{
				gs = new GrapScreen;
			}
			else
			{
				delete gs;
				gs = new GrapScreen;
			}

			if (gs->DoModal())
			{
				
			}
			return 1;//否则会出错
		}
		break;

	case(WM_LBUTTONDOWN):
		if (pMsg->hwnd == GetDlgItem(IDC_BUTTON_GRASP)->m_hWnd)
		{
			HBITMAP hBitmap;
			HINSTANCE  hInstance   =   ::AfxGetInstanceHandle();
			hBitmap =  ::LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP_BUTTON2) );
			((CButton*)GetDlgItem(IDC_BUTTON_GRASP))->SetBitmap(hBitmap);
			oldHcursor = SetCursor(LoadCursorA(NULL, IDC_HAND));

			graspButton=1;
		} 
		break;

	case(WM_LBUTTONUP):
		//if (pMsg->hwnd == GetDlgItem(IDC_BUTTON_GRASP)->m_hWnd)
		if(1==graspButton)
		{
			SetCursor(oldHcursor);

			HBITMAP hBitmap;
			HINSTANCE  hInstance   =   ::AfxGetInstanceHandle();
			hBitmap =  ::LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP_BUTTON) );
			((CButton*)GetDlgItem(IDC_BUTTON_GRASP))->SetBitmap(hBitmap);

			GetCursorPos(&dstPoint);//获得鼠标位置
			dstCwnd = WindowFromPoint(dstPoint)/*->GetForegroundWindow()*/;
			//SetTimer(1,clickTime,0);
			graspButton = 0;
		}
		break;

	case(WM_MOUSEMOVE):
		//if (pMsg->hwnd == GetDlgItem(IDC_BUTTON_GRASP)->m_hWnd)
		//{
		//	HBITMAP hBitmap;
		//	HINSTANCE  hInstance   =   ::AfxGetInstanceHandle();
		//	hBitmap =  ::LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP_BUTTON) );
		//	((CButton*)GetDlgItem(IDC_BUTTON_GRASP))->SetBitmap(hBitmap);
		//}
		break;

	default:
		break;
	}

	return CDialogEx::PreTranslateMessage(pMsg);
}

LRESULT CMFCGraspPointDlg::OnHotKey(WPARAM wParam,LPARAM lParam)//这里是自定义的消息响应函数
{
	// TODO: Add your code here
	//startGrapPoint();
	return 0;
}

int CMFCGraspPointDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialogEx::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码

	return 0;
}


void CMFCGraspPointDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CRect rect;
	CPoint point;
	CPoint clientPoint;
	CDC *pDC = GetWindowDC();//整个窗口
	HDC hDC = ::GetDC(NULL);//指定窗口客户区或整个屏幕
	COLORREF color;
	GetCursorPos(&point);

	CWnd *wnd = WindowFromPoint(point);//获得鼠标所指窗口/控件CWnd
	HWND hwnd = wnd->GetSafeHwnd();
	CWnd *fgwnd = wnd->GetForegroundWindow();//获得鼠标所指窗口CWnd
	CWnd *childwnd = GetWindow(GW_CHILD);

	clientPoint = point;
	wnd->ScreenToClient(&clientPoint);

	if (ifGrapPoint&&(0 == nIDEvent))
	{
		char str[5000]="";
		char className[500]="";
		char windowName[500]="";
		int strLength=0;

		GetClassNameA(hwnd,className,500);//获得窗口类名称
		wnd->GetWindowTextA(windowName,500);//获得窗口标题

		//ScreenToClient(&point);
		//color = pDC->GetPixel(point);//获取整个窗口区域颜色
		//ClientToScreen(&point);
		color = GetPixel(hDC,point.x,point.y);//获取整个屏幕区域颜色

		sprintf(str,
			"x:%d,y:%d\r\nx2:%d,y2:%d\r\nR:%d,G:%d,B:%d\r\nclassName:%s\r\nwindowName:%s\r\nhwnd:%x\r\nwnd:%x\r\nfgwnd:%x\r\ndstCwnd:%x\r\nchildwnd:%x",
			point.x,point.y,clientPoint.x,clientPoint.y,
			GetRValue(color),GetGValue(color),GetBValue(color),
			className,windowName,hwnd,wnd,fgwnd,dstCwnd,childwnd);
		strLength = strlen(str);
		GetDlgItem(IDC_LOCATION)->SetWindowPos(0,0,0,300,200,SWP_NOZORDER | SWP_NOMOVE);//只改变尺寸

		SetDlgItemTextA(IDC_LOCATION,str);
	}

	if (1 == nIDEvent && isScreenClick)
	{
		CPoint pt;  
		GetCursorPos(&pt);
		SetCursorPos(dstPoint.x,dstPoint.y);
		if(dstCwnd && IsWindow(dstCwnd->GetSafeHwnd()))
			dstCwnd->SetWindowPos(0,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE);//设置窗口置顶
		//mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);//点下右键 
		//mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);  //松开右键 
		mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); //点下左键 
		mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);  //松开左键 
		if (fgwnd)
		{
			fgwnd->SetWindowPos(0,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE);
		}
		else if (wnd)
		{
			wnd->SetWindowPos(0,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE);
		}

		SetCursorPos(pt.x,pt.y);
	}

	if (1 == nIDEvent && !isScreenClick)
	{
		dstCwnd->SendMessageA(BM_CLICK);
		//dstCwnd->SendMessageA(WM_LBUTTONDOWN);
		//dstCwnd->SendMessageA(WM_LBUTTONUP);
	}

	::ReleaseDC(NULL, hDC);
	ReleaseDC(pDC);
	CDialogEx::OnTimer(nIDEvent);
}


void CMFCGraspPointDlg::OnBnClickedButtonStop()
{
	// TODO: 在此添加控件通知处理程序代码
	int strLength=0;
	char str[10]="\0";
	strLength = strlen(str);
	GetDlgItem(IDC_LOCATION)->SetWindowPos(0,0,0,5*strLength,50,SWP_NOZORDER | SWP_NOMOVE);
	SetDlgItemTextA(IDC_LOCATION,str);

	if(ifGrapPoint)
	{
		ifGrapPoint=0;
		SetDlgItemTextA(IDC_BUTTON_START,"开始");
		KillTimer(0);
	}
}


void CMFCGraspPointDlg::OnBnClickedButtonHelp()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialgHelp dlgHelp;
	dlgHelp.DoModal();
}

//最小化
void CMFCGraspPointDlg::changeMini()
{
	/*GetDlgItem(IDC_LOCATION)->*/ShowWindow(SW_HIDE);
	/*NotifyIcon.cbSize=sizeof(NOTIFYICONDATA);
	NotifyIcon.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	NotifyIcon.hWnd=m_hWnd;
	lstrcpy(NotifyIcon.szTip,"MediaCraft EMMG");
	NotifyIcon.uCallbackMessage=WM_NC;
	NotifyIcon.uFlags=NIF_ICON | NIF_MESSAGE | NIF_TIP;
	Shell_NotifyIcon(NIM_ADD,&NotifyIcon);*/
}

UINT CMFCGraspPointDlg::OnNotifyFormat(CWnd *pWnd, UINT nCommand)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	return CDialogEx::OnNotifyFormat(pWnd, nCommand);
}


void CMFCGraspPointDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	/*	if (nID==SC_MINIMIZE)
	{
	ShowWindow(SW_HIDE);       
	}
	else*/
	CDialogEx::OnSysCommand(nID, lParam);
}

LRESULT CMFCGraspPointDlg::OnNotifyIcon(WPARAM wParam,LPARAM IParam)
{
	if(IsWindowVisible() && ((IParam == WM_LBUTTONDOWN) || (IParam == WM_RBUTTONDOWN)))
	{
		ShowWindow(SW_HIDE);
	}
	else if ((IParam == WM_LBUTTONDOWN) || (IParam == WM_RBUTTONDOWN))
	{ 
		ModifyStyleEx(0,WS_EX_TOPMOST);
		ShowWindow(SW_SHOW);
	}
	return TRUE;
}

void CMFCGraspPointDlg::OnBnClickedRadioScreen()
{
	// TODO: 在此添加控件通知处理程序代码
	isScreenClick=1;
}


void CMFCGraspPointDlg::OnBnClickedRadiobutton()
{
	// TODO: 在此添加控件通知处理程序代码
	isScreenClick=0;
}


void CMFCGraspPointDlg::OnNMCustomdrawSliderTime(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	clickTime = ((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TIME))->GetPos();
	char timeStr[10] ="";
	itoa(clickTime,timeStr,10);
	GetDlgItem(IDC_STATIC_TIME)->SetWindowTextA(timeStr);
	*pResult = 0;
}


void CMFCGraspPointDlg::OnBnDropDownButtonGrasp(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMBCDROPDOWN pDropDown = reinterpret_cast<LPNMBCDROPDOWN>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}


void CMFCGraspPointDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CDialogEx::OnLButtonDown(nFlags, point);
}




void CMFCGraspPointDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CDialogEx::OnLButtonUp(nFlags, point);
}


void CMFCGraspPointDlg::OnExit()
{
	// TODO: 在此添加命令处理程序代码
	gs->OnExit();
}


void CMFCGraspPointDlg::OnCopy()
{
	// TODO: 在此添加命令处理程序代码
	gs->OnCopy();
}


void CMFCGraspPointDlg::OnSave()
{
	// TODO: 在此添加命令处理程序代码
	gs->OnSave();
}
